package model;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

public class Pallet{
	public String id;
	public String cookieType;
	public final String DEFAULT_LOCATION = "Deep-freeze storage";
	public String location;
	public String producedTime;
	public String deliveredTime;
	public String orderNbr;
	
	public Pallet(String cookieType, String orderNbr){
		this.cookieType = cookieType;
		this.orderNbr = orderNbr;
		producedTime = getCurrentTime();
		location = DEFAULT_LOCATION;
		System.out.println();
	}
	
	private String getCurrentTime(){
		String[] ids = TimeZone.getAvailableIDs(2 * 60 * 60 * 1000);
		SimpleTimeZone pdt = new SimpleTimeZone(2 * 60 * 60 * 1000, ids[0]);
		GregorianCalendar currentDate = new GregorianCalendar(pdt);//TODO: create default producedTime
		String year = currentDate.get(Calendar.YEAR)+"";
		String month = currentDate.get(Calendar.MONTH)+"";
		if(Integer.parseInt(month)<10){
			month = "0"+month;
		}
		String dayOfMonth =currentDate.get(Calendar.DAY_OF_MONTH)+"";
		if(Integer.parseInt(dayOfMonth)<10){
			dayOfMonth = "0"+dayOfMonth;
		}
		return  "'"+year+"-"+month+"-"+dayOfMonth+"'";
	}
	
	public Pallet(String id, String cookieType, String location, String producedTime, String deliveredTime, String orderNbr){
		this.id = id;
		this.cookieType = cookieType;
		this.location = location;
		this.producedTime = producedTime;
		this.deliveredTime = deliveredTime;
		this.orderNbr = orderNbr;
	}
	
	public String maxNbrOfPalletsCode(){
		String sqlStmt = "select nbrOfPallets from OrderedGoods where " +
				"orderNbr="+orderNbr+" and cookieName='"+cookieType+"';";
		return sqlStmt;
	}
	
	public String currentPallNbrForOrder(){
		String sqlStmt = "select count(pallNbr) from Pallets where " +
				"orderNbr="+orderNbr+" and cookieName='"+cookieType+"';";
		return sqlStmt;
	}
	
	public String addPalletCode() {
		String sqlStmt = "insert into Pallets(cookieName, producedTime, orderNbr) values ('"
			+ cookieType +"', "+producedTime+", "+orderNbr+");";
		return sqlStmt;
	}
	
	public String selectNeededIngredients(){
		return "select ingredientName, weight from RecipeSegments where cookieName='"+cookieType+"';";
	}

	@Override
	public String toString() {
		return id+ " : " + DEFAULT_LOCATION +" : "+cookieType+" : "+producedTime;
	}
}
